package com.notes.config.date;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author: huxingxin
 * @date: 2024/12/26
 * 自定义全局的序列化和反序列化器, 处理 HTTP 请求json格式的参数
 * 支持的数据格式：localDateTime, LocalDate, Date
 */
@Configuration
public class JacksonConfig {
    private final String datePattern="yyyy-MM-dd";
    private final String dateTimePattern = "yyyy-MM-dd HH:mm:ss";

    private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateTimePattern);
    private final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(datePattern);
    private final  SimpleDateFormat sdf = new SimpleDateFormat(dateTimePattern);

    private final boolean lenient = false;


    @Bean
    public Jackson2ObjectMapperBuilderCustomizer customizer() {
        // 创建JavaTimeModule并添加自定义的序列化器和反序列化器
        JavaTimeModule module = new JavaTimeModule();

        // 注册自定义的 LocalDateTime 和 LocalDate 反序列化器
        module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter));
        module.addDeserializer(LocalDate.class, new LocalDateDeserializer(dateFormatter));
        module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter));
        module.addSerializer(LocalDate.class, new LocalDateSerializer(dateFormatter));

        // 处理 java.util.Date 类型
        SimpleModule dateModule = new SimpleModule();
        dateModule.addDeserializer(Date.class, new CustomDateDeserializer());
        dateModule.addSerializer(Date.class, new CustomDateSerializer());

        return builder -> {
            builder.modules(module, dateModule);
            // 其他自定义配置
        };
    }

    // 自定义的 java.util.Date 反序列化器和序列化器
    private class CustomDateDeserializer extends JsonDeserializer<Date> {
        @Override
        public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
            String dateStr = p.getText();
            sdf.setLenient(lenient);
            try {
                return sdf.parse(dateStr);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    private class CustomDateSerializer extends JsonSerializer<Date> {
        @Override
        public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            sdf.setLenient(lenient);
            gen.writeString(sdf.format(value));
        }
    }
}
